package com.erp.activity

import androidx.appcompat.app.AlertDialog
import android.os.Environment
import android.util.Log
import android.view.View
import android.widget.AdapterView
import com.erp.R
import com.erp.myAdapter.DownLoagAdapter
import com.erp.util.Dialogutil
import kotlinx.android.synthetic.main.activity_downloag.*
import kotlinx.android.synthetic.main.header_knoya.*
import java.io.File
import kotlin.collections.ArrayList
import java.util.ArrayList as ArrayList1

/**
 * Created by Administrator on 2019/3/22/022.
 * 作者：刘洋
 */
class LingShi1Activity  : BaseActivity(), AdapterView.OnItemLongClickListener, AdapterView.OnItemClickListener, View.OnClickListener {


    var f = ""
    lateinit var adapter : DownLoagAdapter
    lateinit var filenames : MutableList<String>
    override fun getLayoutId(): Int {
        return R.layout.activity_downloag
    }

    override fun initCreate() {
        tv_header_title.text="下载管理"
        iv_header_left.visibility=View.VISIBLE
        iv_header_left.setOnClickListener(this)
        LvDownload.onItemLongClickListener = this
        LvDownload.onItemClickListener = this
        f = Environment.getExternalStorageDirectory().toString() + "/erp/download/"
        filenames  = ArrayList()
        adapter = DownLoagAdapter(filenames,this@LingShi1Activity)
        LvDownload.adapter=adapter
        getfile()
    }

    fun getfile(){
        filenames.clear()
        var file=File(f)
        var files=file.listFiles()
        for (i in 0 .. files.size-2){
            for (j in 0 .. files.size-2){
                if(files[j].lastModified()<files[j+1].lastModified()){
                    var temp=files[j];
                    files[j]=files[j+1];
                    files[j+1]=temp;
                }
            }
        }
        Log.d("getfile","size="+files.size)
        for (i in files.indices) {
            var ss = files[i].absolutePath.split("/".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            filenames.add(ss[ss.size - 1])
        }
        adapter.notifyDataSetChanged()
        Log.d("getfile","size="+files.size)
    }

    override fun onItemLongClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long): Boolean {
        val dialog = AlertDialog.Builder(this@LingShi1Activity)
        dialog.setTitle("提示")
        dialog.setMessage("确定要删除     " + filenames[position])
        dialog.setPositiveButton("确定") { dialog, which ->
            File(f+filenames[position]).delete()
            getfile()
        }
        dialog.setNegativeButton("取消") { dialog, _ -> dialog.dismiss() }
        dialog.show()
        return true
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        Dialogutil.openFileReader(this@LingShi1Activity,f+filenames[position])
    }

    override fun onClick(v: View?) {
        when(v){
            iv_header_left ->{
                finish()
            }
        }
    }
}